      SUBROUTINE CHECK_INT(KFILDO,IA,IB,NVAL,NXY,IS5,NS5,ICLEAN,IBITMAP,
     1                     MISSP,MISSS,JMISSP,JMISSS,IER,
     2                     JER,NDJER,KJER,*)
C
C        MAY      2000   LAWRENCE ORIGINAL CODING
C        JANUARY  2001   GLAHN    COMMENTS
C        NOVEMBER 2001   GLAHN    ADDED JER, NDFER, AND KJER TO
C                                 CALL AND CALLED TRACE
C        DECEMBER 2001   GLAHN    ADDED TEST FOR MISSS=MISSP;
C                                 ADDED KFILDO TO CALL
C        FEBRUARY 2002   GLAHN    MODIFIED COMMENT FOR NVAL
C
C        PURPOSE
C            DETERMINES HOW MANY VALUES ARE PRESENT IN AN INTEGER
C            DATA FIELD BASED ON THE PACKING METHOD AND WHETHER
C            OR NOT A BIT-MAP WAS PROVIDED TO THIS ROUTINE.  THIS
C            ROUTINE ALSO DETERMINES THE EXISTENCE OF PRIMARY
C            AND SECONDARY MISSING VALUES IN THE DATA FIELD.
C
C        DATA SET USE
C           NONE
C
C        VARIABLES
C               IA(K) = IA( ) CONTAINS THE DATA TO BE PACKED INTO
C                       THE GRIB2 MESSAGE (K=1,NVAL).  (INPUT/OUTPUT)
C               IB(K) = THE BIT MAP WHEN ONE IS USED. IT INDICATES
C                       WHERE MISSING VALUES WERE LOCATED IN THE
C                       DATA FIELD AFTER THE MISSING VALUES
C                       HAVE BEEN REMOVED. THERE IS A ONE-TO-ONE
C                       CORRESPONDENCE BETWEEN THE VALUES IN THE
C                       BIT-MAP AND THE VALUES IN THE DATA FIELD
C                       BEFORE THE MISSING VALUES ARE REMOVED.
C                       A 0 IN THE BIT-MAP INDICATES THE POSITION
C                       OF A MISSING VALUE, A 1 INDICATES THE
C                       POSITION OF A VALID VALUE. IN THIS ROUTINE
C                       THE BIT-MAP IS USED TO GET A COUNT OF THE
C                       NUMBER OF VALID VALUES (NVAL).  (INPUT)
C                NVAL = THE NUMBER OF VALUES IN IA( ).  THIS IS
C                       THE NUMBER OF NON-MISSING VALUES FOR SIMPLE
C                       WITH A BIT MAP; NXY OTHERWISE.  (OUTPUT)
C                 NXY = DIMENSION OF IA( ) AND IB( ).  (INPUT)
C              IS5(K) = THE VALUES ASSOCIATED WITH SECTION 5, KEYED
C                       TO THE OCTET NUMBER (K=1,NS5).  THE ELEMENTS
C                       USED IN THIS ROUTINE ARE:
C                       IS5(10), TEMPLATE NUMBER:
C                       0 = SIMPLE
C                       1 = NOT SUPPORTED
C                       2 = COMPLEX
C                       3 = SPATIAL DIFFERENCING
C                       (INPUT)
C                       IS5(23), PROVISION FOR MISSING
C                       VALUES WHEN PROCESS IS NOT SIMPLE:
C                       0 = NO EXPLICIT MISSING VALUES IN GRID
C                           [A( ) OR IA( )]
C                       1 = PRIMARY MISSING VALUES ONLY ARE POSSIBLE
C                       2 = PRIMARY AND SECONDARY MISSING VALUES ARE
C                           POSSIBLE
C                       (INPUT/OUTPUT)
C                 NS5 = THE DIMENSION OF IS5( ).  (INPUT)
C              ICLEAN = 1 WHEN THERE ARE NO MISSING VALUES IN IA( ).
C                       0 OTHERWISE.  (INPUT/OUTPUT)
C             IBITMAP = 1 WHEN THERE IS A BITMAP IN IB( , ).
C                       0 OTHERWISE.  (INPUT)
C               MISSP = WHEN MISSING POINTS CAN BE PRESENT IN THE DATA,
C                       THEY WILL HAVE THE VALUE MISSP.  (INPUT)
C               MISSS = THE SECONDARY MISSING VALUE.  (INPUT)
C              JMISSP = FLAG INDICATING WHETHER OR NOT THERE
C                       ARE PRIMARY MISSING VALUES IN THIS ROUTINE.
C                       TRUE = YES; FALSE = NO.  (LOGICAL) (OUTPUT)
C              JMISSS = FLAG INDICATING WHETHER OR NOT THERE
C                       ARE SECONDARY MISSING VALUES IN THIS ROUTINE.
C                       TRUE = YES; FALSE = NO.  (LOGICAL) (OUTPUT)
C                 IER = CONTAINS ANY ERROR MESSAGES GENERATED BY THIS
C                       ROUTINE. POSSIBLE ERROR CODES ARE:
C                        901 - AN INVALID COMBINATION OF ICLEAN, 
C                              IS5(10) (PACKING METHOD), AND IS5(23)
C                              (MISSING VALUES) WAS SPECIFIED.
C                              SEE EXTERNAL DOCUMENTATION.
C            JER(J,K) = RETURN STATUS CODES AND SEVERITY LEVELS
C                       (J=1,NDJER)(K=1,2).  VALUES CAN COME FROM
C                       SUBROUTINES; OTHERWISE: 0 = GOOD RETURN.
C                       (INPUT/OUTPUT)
C               NDJER = DIMENSION OF JER( ).  (INPUT)
C                KJER = NUMBER OF VALUES IN JER( ).  (INPUT/OUTPUT)
C                   * = ALTERNATE ERROR RETURN.
C
C        LOCAL VARIABLES
C                   K = A LOOPING/ARRAY INDEXING VARIABLE.
C
C        NON SYSTEM SUBROUTINES CALLED
C          NONE 
C
      LOGICAL JMISSP,JMISSS
C
      DIMENSION IA(NXY),IB(NXY),IS5(NS5)
      DIMENSION JER(NDJER,2)
C
      IF(IS5(23).EQ.2)THEN
C
         IF(MISSS.EQ.MISSP)THEN
C              MISSS IS EQUAL TO MISSP WHEN BOTH TYPES OF MISSINGS
C              ARE INDICATED.  SET IS5(23) TO INDICATE ONLY PRIMARY
C              MISSINGS.
            IS5(23)=1
            CALL PK_TRACE(KFILDO,JER,NDJER,KJER,916,1)
         ENDIF
C  
      ENDIF
C        
      JMISSP=.FALSE.
      JMISSS=.FALSE.
      IER=0
      NVAL=0
C
      IF(ICLEAN.EQ.1)THEN
C
C           THERE ARE NO MISSING VALUES IN THE DATA FIELD.
C           IF THIS IS SIMPLE PACKING, CHECK THE BIT-MAP
C           TO GET A COUNT OF THE NUMBER OF VALID VALUES
C           IN THE FIELD. IF THERE IS NO BIT-MAP, THEN
C           THERE MUST BE NO MISSING VALUES.
C
         IF(IS5(10).EQ.0)THEN
C
            IF(IBITMAP.EQ.1)THEN
C
               DO 10 K=1,NXY
                  IF(IB(K).EQ.1)NVAL=NVAL+1
 10            CONTINUE
C
            ELSE
               NVAL=NXY
            ENDIF
C
         ELSE
            NVAL=NXY
         ENDIF
C
      ELSEIF((IS5(10).GE.2).AND.(IS5(23).EQ.1))THEN
C           THE USER MUST HAVE SUPPLIED IS5(23) WHEN
C           IS5(10).GE.2.
C
C           COMPLEX PACKING IS BEING USED AND THERE CAN BE
C           PRIMARY MISSING VALUES. NVAL ALWAYS EQUALS NXY
C           WITH COMPLEX PACKING. IF A BIT-MAP WAS PROVIDED
C           THEN THE DATA FIELD HAS ALREADY BEEN EXPANDED
C           TO INCLUDE THE MISSING VALUES.
         NVAL=NXY
C
         DO 30 K=1,NXY
C
            IF(IA(K).EQ.MISSP)THEN
               JMISSP=.TRUE.
               EXIT
            ENDIF
C
 30      CONTINUE
C
         IF(.NOT.JMISSP)THEN
            ICLEAN=1
            IS5(23)=0
C              THIS OVERRIDES USER INPUT INTO IS5(23).
            CALL PK_TRACE(KFILDO,JER,NDJER,KJER,909,1)
         ENDIF
C
      ELSEIF((IS5(10).GE.2).AND.(IS5(23).EQ.2))THEN
C           THE USER MUST HAVE SUPPLIED IS5(23) WHEN
C           IS5(10).GE.2.
C
C           COMPLEX PACKING IS BEING USED AND THERE CAN
C           BE BOTH PRIMARY AND SECONDARY MISSING VALUES. 
C           NVAL ALWAYS EQUALS NXY WITH COMPLEX PACKING.
C           IF A BIT-MAP WAS PROVIDED THEN THE DATA
C           FIELD HAS ALREADY BEEN EXPANDED TO INCLUDE
C           THE MISSING VALUES.
         NVAL=NXY
C
         DO 40 K=1,NXY
            IF(IA(K).EQ.MISSP)JMISSP=.TRUE.
            IF(IA(K).EQ.MISSS)JMISSS=.TRUE.
            IF(JMISSP.AND.JMISSS)EXIT 
 40      CONTINUE
C
         IF(.NOT.JMISSP.AND..NOT.JMISSS)THEN
            ICLEAN=1
            IS5(23)=0
C              THIS OVERRIDES USER INPUT INTO IS5(23).
            CALL PK_TRACE(KFILDO,JER,NDJER,KJER,909,1)
         ELSE IF(.NOT.JMISSS)THEN
            IS5(23)=1
C              THIS OVERRIDES USER INPUT INTO IS5(23).
            CALL PK_TRACE(KFILDO,JER,NDJER,KJER,910,1)
         ENDIF 
C
      ELSE
C
C           AN INVALID COMBINATION OF ICLEAN, IS5(10), AND IS5(23)
C           HAS BEEN SPECIFIED.  THE USER MUST CHECK THE INPUT TO
C           THE PACKER.
         IER=901
      ENDIF
C
      IF(IER.NE.0)RETURN1
C
      RETURN
      END
